package com.jk.bguser.service;

import com.jk.bguser.dao.UserDao;
import com.jk.bguser.entity.CouponEntity;
import com.jk.damaicommon.entity.TicketEntity;
import com.jk.bguser.entity.UserEntity;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.UUID;

/**
 * @author 赵优秀
 * @version 1.0.0
 * @ClassName UserService.java
 * @Description TODO
 * @createTime 2021年09月01日 10:53:00
 */
@Service
public class UserService {
    @Resource
    private UserDao userDao;

    //查询
    public List<UserEntity> findUser(UserEntity userEntity) {
        return userDao.findUser(userEntity);
    }

    //批量删除
    public void delAll(String ids) {
        userDao.delAll(ids);
    }

    /**
     * @throws
     * @title
     * @description
     * @author 赵优秀
     * @updateTime 2021/9/2 9:15
     */
    //批量冻结
    public void freezeAll(String ids) {
        userDao.freezeAll(ids);
    }

    //删除
    public void del(Integer id) {
        userDao.del(id);
    }

    //冻结
    public void freezeUser(Integer id) {
        userDao.freezeUser(id);
    }

    //解冻
    public void UnFreezeUser(Integer id) {
        userDao.UnFreezeUser(id);
    }

    //回显
    public UserEntity getByid(Integer id) {
        return userDao.getByid(id);
    }

    //修改
    public void updateUser(UserEntity userEntity) {
        userDao.updateUser(userEntity);
    }

    //冻结查询
    public List<UserEntity> findFreezeUser(UserEntity userEntity) {
        return userDao.findFreezeUser(userEntity);
    }

    //删除查询
    public List<UserEntity> findDelUser(UserEntity userEntity) {
        return userDao.findDelUser(userEntity);
    }

    //恢复用户
    public void onUser(Integer id) {
        userDao.onUser(id);
    }

    //批量解冻
    public void onFreezeAll(String ids) {
        userDao.onFreezeAll(ids);
    }
    //查询用户优惠券的数量
    public List<UserEntity> findCoupon() {
        List<UserEntity>  list=userDao.finCoupon();
        for (UserEntity  uu: list) {
            //查询数量
          List<CouponEntity> cc= userDao.getCount(uu.getUserId());
          uu.setCouponCount(cc.size());
        }
        return list;
    }
    /**
     * @title 查询拥有的优惠券
     * @description
     * @author 赵优秀
     * @updateTime 2021/9/15 21:19
     * @throws
     */
    public List<TicketEntity> grantCoupon() {
        List<TicketEntity> list = userDao.grantCoupon();
        System.out.println(list);
        return list;
    }
    /**
     * @title 发放优惠券
     * @description
     * @author 赵优秀
     * @updateTime 2021/9/16 10:28
     * @throws
     * @return
     */
    public String grant(TicketEntity ticket) {
        //新增发放优惠券
        String msg="";
        CouponEntity coupon=new CouponEntity();
        String a=UUID.randomUUID().toString();
        coupon.setGenreId(ticket.getTickId());
        coupon.setUserId(ticket.getUserId());
      String b=  a.toString().replaceAll("-","");
        coupon.setCouponNum(b);
        userDao.addCoupon(coupon);
        //查询一下优惠券的所剩数量
        TicketEntity tt=  userDao.getremainCount(ticket.getTickId());
        //判断剩余票数不等于0
        if (tt.getRemainCount()!=0){
            //修改优惠券的数量
            userDao.updateCount(ticket.getTickId());
            msg+="发放成功";
        }else {
            msg+="没有优惠券了";
        }
        return msg;
    }
    /**
     * @title 查询用户所有的优惠券
     * @description
     * @author 赵优秀
     * @updateTime 2021/9/16 10:29
     * @throws
     */
    public List<TicketEntity> userCoupon(Integer id) {
        return  userDao.userCoupon(id);
    }
}
